﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Microsoft.AspNet.Identity;
using GPS.GPS_DAL;
using System.Globalization;
using GPS.Models;


namespace GPS.Controllers.GPS_NghiepVu
{
    public class GPS_LichHoc_THController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();

            string userId = User.Identity.GetUserId();

            ApplicationUser _currentUser = dbUser.Users.Find(userId);

            return _currentUser;
        }

        //
        // GET: /GPS_LichHoc/

        public ActionResult Index(ViewModelDKH_LH1 SearchModel)
        {
            SearchModel.KH_LH_DVGTVT_MA = this.getCurrentUser().MA_TINHTP;

            initialCategorySearchAction(SearchModel);
            return View();
        }

        //
        // Search: /GPS_LichHoc/

        public ActionResult Search(ViewModelDKH_LH1 SearchModel)
        {
            if (SearchModel == null)
                return View(SearchModel);

            if (SearchModel.KH_LH_DVGTVT_MA == null)
                SearchModel.KH_LH_DVGTVT_MA = "40";

            string _DVGTGT = this.getCurrentUser().MA_TINHTP;

            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40" && o.DVGTVT_ACTIVE == "1"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;

            //KH_MAs
            var khoahocs = Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN_LH1(SearchModel.KH_LH_DVGTVT_MA, SearchModel.KH_LH_KH_MA);
            ViewBag.KH_MAs = new SelectList(khoahocs, "KH_MA", "KH_TEN");
            //var gvs = _db.Filter<DGV>(o => o.GV_DVGTVT_MA == SearchModel.KH_LH_DVGTVT_MA).Select(
            //   r => new
            //   {
            //       GV_MA = r.GV_MA,
            //       GV_TEN = r.GV_HODEM + " " + r.GV_TEN,
            //   });

            var ds1 = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_PT_IN_KH(SearchModel.KH_LH_DVGTVT_MA, SearchModel.KH_LH_KH_MA);

            var lstPTinKH = new List<JsonDataDKH_PTinKH>();
            lstPTinKH = GPS_Library.MapDataTableToList<JsonDataDKH_PTinKH>(ds1.Tables[1]);
            ViewBag.PT_MAs = new SelectList(lstPTinKH, "PT_MA", "PT_MA"); //new SelectList(_db.Filter<DPT>(o => o.PT_DVGTVT_MA == SearchModel.KH_LH_PT_BIENSO), "PT_BIENSO", "PT_BIENSO");

            var ds = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_GV_IN_KH_LT(SearchModel.KH_LH_DVGTVT_MA, SearchModel.KH_LH_KH_MA);

            //giang vien
            var lstGV = new List<JsonDataDKH_GVinKH_LT>();
            lstGV = GPS_Library.MapDataTableToList<JsonDataDKH_GVinKH_LT>(ds.Tables[1]);
            // var districts = lstPTinKH.Select(a => "<option value='" + a.GV_MA + "'>" + a.GV_HOTEN + "</option>");
            var gvs = lstGV;
            ViewBag.GV_MAs = new SelectList(gvs, "GV_MA", "GV_HOTEN");

            if (SearchModel.KH_LH_PT_BIENSO != null && !string.IsNullOrWhiteSpace(SearchModel.KH_LH_PT_BIENSO))
            {


                var gvs1 = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_GV_IN_PT(SearchModel.KH_LH_DVGTVT_MA, SearchModel.KH_LH_KH_MA, SearchModel.KH_LH_PT_BIENSO);
                ViewBag.GV_MAs = new SelectList(gvs1, "GV_MA", "GV_HOTEN");
            }




            var htdts = _db.GetAll<SHTDAOTAO>();

            ViewBag.HANGDT_MAs = new SelectList(_db.Filter<SHANGDT>(o => o.HANGDT_RANK > 4), "HANGDT_MA", "HANGDT_TEN");

            var entities = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_SEARCH_KH_LH_1_TH(SearchModel.KH_LH_DVGTVT_MA
                                                                                , SearchModel.KH_LH_KH_MA
                                                                                , SearchModel.KH_LH_GV_MA
                                                                                , SearchModel.KH_LH_PT_BIENSO
                                                                                , SearchModel.HANGDT_MA
                                                                                , SearchModel.KH_LH_TUNGAY
                                                                                , SearchModel.KH_LH_DENNGAY
                                                                                );

            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = entities.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);

            ViewBag.TotalRow = entities.Count();

            return View(SearchModel);
        }

        #region"Ajax"
        [HttpPost]
        public ActionResult getKH_MAs3(string ma_DVGT)
        {
            var districts = Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN(ma_DVGT).Select(a => "<option value='" + a.KH_MA + "'>" + a.KH_TEN + "</option>");
            var giangvien = _db.Filter<DGV>(o => o.GV_DVGTVT_MA == ma_DVGT).Select(a => "<option value='" + a.GV_MA + "'>" + a.GV_HODEM + " " + a.GV_TEN + "</option>");
            var phuongtien = _db.Filter<DPT>(o => o.PT_DVGTVT_MA == ma_DVGT).Select(a => "<option value='" + a.PT_BIENSO + "'>" + a.PT_BIENSO + "</option>");
            var data = new JsonStringTH();
            data.strKhoaHoc = string.Join("", districts);
            data.strgiangvien = string.Join("", giangvien);
            data.strphuongtien = string.Join("", phuongtien);
            return Json(new { pts = data }, JsonRequestBehavior.AllowGet);
            //return Content(content);
        }

        public ActionResult getGV_IN_KH_MA_LTs2(string Ma_DVGT, string Ma_KH)
        {

            var ds = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_GV_IN_KH_LT(Ma_DVGT, Ma_KH);

            var lstPTinKH = new List<JsonDataDKH_GVinKH_LT>();
            lstPTinKH = GPS_Library.MapDataTableToList<JsonDataDKH_GVinKH_LT>(ds.Tables[1]);
            var districts = lstPTinKH.Select(a => "<option value='" + a.GV_MA + "'>" + a.GV_HOTEN + "</option>");

            var objKH = new JsonDataDKH_GVinKH_LT_1();

            objKH.OptionsGV = string.Join("", districts);

            return Json(new { pts = objKH }, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult getPT_IN_KH_MAs3(string Ma_DVGT, string Ma_KH)
        {
            //get phương tiện trong khóa học


            var ds = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_PT_IN_KH(Ma_DVGT, Ma_KH);

            var lstPTinKH = new List<JsonDataDKH_PTinKH>();
            lstPTinKH = GPS_Library.MapDataTableToList<JsonDataDKH_PTinKH>(ds.Tables[1]);
            var districts = lstPTinKH.Select(a => "<option value='" + a.PT_MA + "'>" + a.PT_MA + "</option>");


            return Content(string.Join("", districts));
        }

        public ActionResult getGV_IN_KH_MAs3(string Ma_DVGT, string Ma_KH)
        {

            JsonDataDKH_GVs dkh_gvs = new JsonDataDKH_GVs();
            dkh_gvs.lstGV_KH_IN = new List<JsonDataDKH_GVinPT>();
            dkh_gvs.lstGV_KH_NOTIN = new List<JsonDataDKH_GVinPT>();
            DataSet ds = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_GV_IN_NOTIN_KH(Ma_DVGT, Ma_KH, "1");
            if (ds.Tables[0].Rows.Count > 0)
            {
                dkh_gvs.lstGV_KH_IN = GPS_Library.MapDataTableToList<JsonDataDKH_GVinPT>(ds.Tables[0]);
            }



            //  dkh_gvs.lstGV_KH_NOTIN=
            var districts = dkh_gvs.lstGV_KH_IN.Select(a => "<option value='" + a.GV_MA + "'>" + a.GV_HOTEN + "</option>");
            return Content(string.Join("", districts));
        }

        public ActionResult GPS_GET_GV_IN_PT3(string Ma_DVGT, string Ma_KH, string Ma_PT)
        {
            JsonDataDKH_GV_HV_IN_PT gv_hv_pts = new JsonDataDKH_GV_HV_IN_PT();
            gv_hv_pts.lstGV_IN_PT = new List<JsonDataDKH_GVinPT>();
            gv_hv_pts.lstHV_IN_PT = new List<JsonDataDKH_HVinKH>();



            gv_hv_pts.lstGV_IN_PT = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_GV_IN_PT(Ma_DVGT, Ma_KH, Ma_PT);

            var districts = gv_hv_pts.lstGV_IN_PT.Select(a => "<option value='" + a.GV_MA + "'>" + a.GV_HOTEN + "</option>");
            return Content(string.Join("", districts));
        }


        #endregion

        public ActionResult List(string KH_LH_DVGTVT_MA, string KH_LH_KH_MA, string KH_LH_GV_MA, string KH_LH_PT_BIENSO, string HANGDT_MA, int? Page)
        {
            ViewModelDKH_LH1 SearchModel = new ViewModelDKH_LH1();
            if (string.IsNullOrWhiteSpace(KH_LH_DVGTVT_MA))
                KH_LH_DVGTVT_MA = this.getCurrentUser().MA_TINHTP;
            SearchModel.KH_LH_DVGTVT_MA = KH_LH_DVGTVT_MA;
            SearchModel.KH_LH_KH_MA = KH_LH_KH_MA;
            SearchModel.KH_LH_GV_MA = KH_LH_GV_MA;
            SearchModel.KH_LH_PT_BIENSO = KH_LH_PT_BIENSO;
            SearchModel.HANGDT_MA = HANGDT_MA;
            SearchModel.Page = Page;

            var entities = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_KH_LH_1_TH(SearchModel.KH_LH_DVGTVT_MA
                                                                                , SearchModel.KH_LH_KH_MA
                                                                                , SearchModel.KH_LH_GV_MA
                                                                                , SearchModel.KH_LH_PT_BIENSO
                                                                                , SearchModel.HANGDT_MA
                                                                                );

            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = entities.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);

            ViewBag.TotalRow = entities.Count();
            return PartialView("_List", SearchModel);
        }



        [HttpGet]
        public ActionResult Create()
        {
            initialCategoryCreateAction();
            CrudModelDKH_LH1_TH model = new CrudModelDKH_LH1_TH();
            string _dvgt_ma = this.getCurrentUser().MA_TINHTP;
            return PartialView("_Create", model);
        }

        [HttpPost]
        public ActionResult Create(CrudModelDKH_LH1_TH model)
        {
            if (ModelState.IsValid)
            {
                model.KH_LH_ACTIVE = "1";
                model.KH_LH_NGUOINHAP = User.Identity.Name;
                model.KH_LH_NGAYNHAP = DateTime.Now;
                Singleton<GPS_HocVienDAO>.Inst.GPS_KH_LH_1_TH_INSERT(model);

                return Json(new { success = true });

            }
            initialCategoryCreateAction();
            return Json(model, JsonRequestBehavior.AllowGet);

        }

        public ActionResult Edit(decimal id)
        {
            string Ma_DVGT = getCurrentUser().MA_TINHTP;
            DKH_LH1 model = Singleton<GPS_HocVienDAO>.Inst.GPS_KH_LH_1_TH_DETAIL(Ma_DVGT, string.Empty, id);

            CrudModelDKH_LH1_TH _model = new CrudModelDKH_LH1_TH();
            GPS.COMMON.Helpers.CopyObject<CrudModelDKH_LH1_TH>(model, ref _model, new List<string> { "KH_LH_ACTIVE" });

            initialCategoryEditAction(_model);
            return PartialView("_Edit", _model);

        }

        [HttpPost]
        public ActionResult Edit(CrudModelDKH_LH1_TH model)
        {
            if (ModelState.IsValid)
            {
                model.KH_LH_ACTIVE = "1";
                model.KH_LH_NGUOISUA = User.Identity.Name;
                model.KH_LH_NGAYSUA = DateTime.Now;
                Singleton<GPS_HocVienDAO>.Inst.GPS_KH_LH_1_TH_UPDATE(model);

                return Json(new { success = true });

            }
            initialCategoryEditAction(model);
            return Json(model, JsonRequestBehavior.AllowGet);

        }

        public ActionResult Delete(decimal id)
        {

            string Ma_DVGT = getCurrentUser().MA_TINHTP;
            DKH_LH1 model = Singleton<GPS_HocVienDAO>.Inst.GPS_KH_LH_1_TH_DETAIL(Ma_DVGT, string.Empty, id);

            CrudModelDKH_LH1_TH _model = new CrudModelDKH_LH1_TH();
            GPS.COMMON.Helpers.CopyObject<CrudModelDKH_LH1_TH>(model, ref _model, new List<string> { "KH_LH_ACTIVE" });

            initialCategoryEditAction(_model);
            return PartialView("_Delete", _model);
        }


        //
        // POST: /Phone/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(decimal id)
        {
            string Ma_DVGT = getCurrentUser().MA_TINHTP;
            Singleton<GPS_HocVienDAO>.Inst.GPS_KH_LH_1_TH_DELETE(Ma_DVGT, string.Empty, string.Empty, string.Empty, id);
            return Json(new { success = true });
        }


        #region detail
        public ActionResult Detail(decimal id)
        {

            string Ma_DVGT = getCurrentUser().MA_TINHTP;
            DKH_LH1 model = Singleton<GPS_HocVienDAO>.Inst.GPS_KH_LH_1_TH_DETAIL(Ma_DVGT, string.Empty, id);

            CrudModelDKH_LH1_TH _model = new CrudModelDKH_LH1_TH();
            GPS.COMMON.Helpers.CopyObject<CrudModelDKH_LH1_TH>(model, ref _model, new List<string> { "KH_LH_ACTIVE" });

            initialCategoryEditAction(_model);
            return PartialView("_Detail", _model);
        }

        public ActionResult GetDSDetail(decimal id)
        {

            string Ma_DVGT = getCurrentUser().MA_TINHTP;
            List<ModelDKH_LH1> model = Singleton<GPS_HocVienDAO>.Inst.GPS_KH_LH_1_TH_GET_BY_IDTH(Ma_DVGT, string.Empty, id);
            ViewBag.TUNGAY = model[0].KH_LH_TUNGAY.Value.ToString("dd/MM/yyyy");
            ViewBag.DENNGAY = model[0].KH_LH_DENNGAY.Value.ToString("dd/MM/yyyy");
            ViewBag.TUAN = model[0].KH_LH_TUAN;
            ViewBag.THANG = model[0].KH_LH_THANG;
            return PartialView("_DetailListPTGV", model);
        }



        public ActionResult HocVienPhuongTien(decimal id, string id2)
        {
            string _KH_LH_KH_MA = id2.Replace("%3A", ":");
            var query =
                      from kh_pt in _db.GetDbSet<DKH_PT>()
                      join hv in _db.GetDbSet<DHV>() on kh_pt.KH_PT_HV_MA equals hv.HV_MA
                      where (kh_pt.KH_PT_KH_LH_ID == id && kh_pt.KH_PT_PT_MA == _KH_LH_KH_MA)
                      select new
                      {
                          hv.ID,
                          hv.HV_MA,
                          hv.HV_DVGTVT_MA,
                          hv.HV_KH_MA,
                          hv.HV_HANGDT_MA,
                          hv.HV_HANGDT_RANK,
                          hv.HV_HODEM,
                          hv.HV_TEN,
                          hv.HV_GIOITINH,
                          hv.HV_QUOCTICH_MA,
                          hv.HV_NGAYSINH,
                          hv.HV_NOITT,
                          hv.HV_NOITT_DVHC_MA,
                          hv.HV_NOITT_DVHC_DVQL,
                          hv.HV_NOICT,
                          hv.HV_NOICT_DVHC_MA,
                          hv.HV_NOICT_DVHC_DVQL,
                          hv.HV_SOCMT,
                          hv.HV_NGAYCAPCMT,
                          hv.HV_NOICAPCMT,
                          hv.HV_MOTA,
                          hv.HV_ACTIVE,
                          hv.HV_NGAYNHAP,
                          hv.HV_NGUOINHAP,
                          hv.HV_NGAYSUA,
                          hv.HV_NGUOISUA
                      };
            List<DHV> entities = new List<DHV>();
            foreach (var hv in query)
            {

                entities.Add(new DHV  //Added UsageCPU  
                {
                    ID = hv.ID,
                    HV_MA = hv.HV_MA,
                    HV_DVGTVT_MA = hv.HV_DVGTVT_MA,
                    HV_KH_MA = hv.HV_KH_MA,
                    HV_HANGDT_MA = hv.HV_HANGDT_MA,
                    HV_HANGDT_RANK = hv.HV_HANGDT_RANK,
                    HV_HODEM = hv.HV_HODEM,
                    HV_TEN = hv.HV_TEN,
                    HV_GIOITINH = hv.HV_GIOITINH,
                    HV_QUOCTICH_MA = hv.HV_QUOCTICH_MA,
                    HV_NGAYSINH = hv.HV_NGAYSINH,
                    HV_NOITT = hv.HV_NOITT,
                    HV_NOITT_DVHC_MA = hv.HV_NOITT_DVHC_MA,
                    HV_NOITT_DVHC_DVQL = hv.HV_NOITT_DVHC_DVQL,
                    HV_NOICT = hv.HV_NOICT,
                    HV_NOICT_DVHC_MA = hv.HV_NOICT_DVHC_MA,
                    HV_NOICT_DVHC_DVQL = hv.HV_NOICT_DVHC_DVQL,
                    HV_SOCMT = hv.HV_SOCMT,
                    HV_NGAYCAPCMT = hv.HV_NGAYCAPCMT,
                    HV_NOICAPCMT = hv.HV_NOICAPCMT,
                    HV_MOTA = hv.HV_MOTA,
                    HV_ACTIVE = hv.HV_ACTIVE,
                    HV_NGAYNHAP = hv.HV_NGAYNHAP,
                    HV_NGUOINHAP = hv.HV_NGUOINHAP,
                    HV_NGAYSUA = hv.HV_NGAYSUA,
                    HV_NGUOISUA = hv.HV_NGUOISUA
                }

                );


            }
            ViewBag.PT_BIENSO = id2;
            return PartialView("_HocVienPhuongTien", entities);
        }

        #endregion

        #region"Ajax"

        public ActionResult getGV_IN_KH_MA_THs(string Ma_DVGT, string Ma_KH)
        {
            //get phương tiện trong khóa học
            Ma_DVGT = getCurrentUser().MA_TINHTP;

            var ds = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_GV_IN_KH_TH(Ma_DVGT, Ma_KH);

            var lstPTinKH = new List<JsonDataDKH_GVinKH_TH>();
            lstPTinKH = GPS_Library.MapDataTableToList<JsonDataDKH_GVinKH_TH>(ds.Tables[1]);

            string str = "";
            for (int i = 0; i < lstPTinKH.Count; i++)
            {
                var a = lstPTinKH[i];
                str = str + "<li class='list-group-item'><input type='checkbox' name='chk_" + i.ToString() + "' style='font-weight: bold;font-size: 12px' checked value='" + a.PT_MA + "#" + a.GV_MA + "'</>" + a.PT_MA + "-" + a.GV_HOTEN + "</li>";
            }

            var objKH = GPS_Library.MapDataTableToList<JsonDataDKH_GVinKH_TH_1>(ds.Tables[0]);


            objKH[0].OptionsWeek = this.GPS_GET_WEEK_BY_KH(Ma_KH);
            objKH[0].SO_HOVIEN = _db.GetDbSet<DHV>().Count(o => o.HV_KH_MA == Ma_KH).ToString();
            objKH[0].OptionsPT_GV = str;

            return Json(new { pts = objKH[0] }, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult getGV_IN_KH_BY_HTHUCDT_THs(string Ma_DVGT, string Ma_KH)
        {

            //get giảng viên trong khóa học
            Ma_DVGT = getCurrentUser().MA_TINHTP;
            var lstPTinKH = new List<JsonDataDKH_GVinPT>();
            lstPTinKH = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_PT_IN_KH_BY_HT_TH(Ma_DVGT, Ma_KH, "TH");

            string str = "";
            for (int i = 0; i < lstPTinKH.Count; i++)
            {
                var a = lstPTinKH[i];
                str = str + "<li class='list-group-item'><input type='checkbox' name='chk_" + i.ToString() + "' style='font-weight: bold;font-size: 12px' checked value='" + a.PT_MA + "#" + a.GV_MA + "'</>" + a.PT_MA + "-" + a.GV_HOTEN + "</li>";
            }

            return Content(str);
        }


        public ActionResult GPS_GET_DATE_BY_WEEK(string Week, string StartDate, string EndDate)
        {
            DateTime dtStartDate = DateTime.ParseExact(StartDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
            DateTime dtEndDate = DateTime.ParseExact(EndDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);

            var intWeek = int.Parse(Week);
            DateRange dateRange = GPS_Library.GET_WEEK_DATERANGE(dtStartDate, dtEndDate)[intWeek];
            return Json(new { dateranges = dateRange }, JsonRequestBehavior.AllowGet);
        }

        public string GPS_GET_WEEK_BY_KH(string Ma_KH)
        {
            //get Week by khóa học
            string Ma_DVGT = getCurrentUser().MA_TINHTP;
            var kh = _db.GetOne<DKH>(o => o.KH_DVGTVT_MA == Ma_DVGT && o.KH_MA == Ma_KH);
            var dictWeekinDate = GPS_Library.GET_WEEK_DATERANGE(kh.KH_NGAYKG.Value, kh.KH_NGAYBG.Value);
            var districts = dictWeekinDate.Keys.Select(a => "<option value='" + a.ToString() + "'>Tuần " + a.ToString() + "</option>");
            return string.Join("", districts);
        }

        [HttpPost]
        public ActionResult getPT_IN_KH_MAs2(string Ma_KH)
        {
            //get phương tiện trong khóa học
            string Ma_DVGT = getCurrentUser().MA_TINHTP;

            var ds = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_PT_IN_KH(Ma_DVGT, Ma_KH);

            var lstPTinKH = new List<JsonDataDKH_PTinKH>();
            lstPTinKH = GPS_Library.MapDataTableToList<JsonDataDKH_PTinKH>(ds.Tables[1]);
            var districts = lstPTinKH.Select(a => "<option value='" + a.PT_MA + "'>" + a.PT_MA + "</option>");


            return Content(string.Join("", districts));
        }

        public ActionResult getGV_IN_KH_MAs(string Ma_KH)
        {
            string Ma_DVGT = getCurrentUser().MA_TINHTP;
            JsonDataDKH_GVs dkh_gvs = new JsonDataDKH_GVs();
            dkh_gvs.lstGV_KH_IN = new List<JsonDataDKH_GVinPT>();
            dkh_gvs.lstGV_KH_NOTIN = new List<JsonDataDKH_GVinPT>();
            DataSet ds = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_GV_IN_NOTIN_KH(Ma_DVGT, Ma_KH, "1");
            if (ds.Tables[0].Rows.Count > 0)
            {
                dkh_gvs.lstGV_KH_IN = GPS_Library.MapDataTableToList<JsonDataDKH_GVinPT>(ds.Tables[0]);
            }



            //  dkh_gvs.lstGV_KH_NOTIN=
            var districts = dkh_gvs.lstGV_KH_IN.Select(a => "<option value='" + a.GV_MA + "'>" + a.GV_HOTEN + "</option>");
            return Content(string.Join("", districts));
        }

        public ActionResult GPS_GET_GV_IN_PT(string Ma_KH, string Ma_PT)
        {
            JsonDataDKH_GV_HV_IN_PT gv_hv_pts = new JsonDataDKH_GV_HV_IN_PT();
            gv_hv_pts.lstGV_IN_PT = new List<JsonDataDKH_GVinPT>();
            gv_hv_pts.lstHV_IN_PT = new List<JsonDataDKH_HVinKH>();

            string Ma_DVGT = getCurrentUser().MA_TINHTP;

            gv_hv_pts.lstGV_IN_PT = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_GV_IN_PT(Ma_DVGT, Ma_KH, Ma_PT);

            var districts = gv_hv_pts.lstGV_IN_PT.Select(a => "<option value='" + a.GV_MA + "'>" + a.GV_HOTEN + "</option>");
            return Content(string.Join("", districts));
        }

        #endregion

        #region Function



        private bool IsBooking(CrudModelDKH_LH1_TH _model)
        {
            var IsBooking = false;
            if (_model != null)
            {
                IsBooking = _db.EntityExists<DKH_LH>(
                b => (b.KH_LH_GV_MA == _model.KH_LH_GV_MA || b.KH_LH_PT_BIENSO == _model.KH_LH_PT_BIENSO)
                    && b.KH_LH_TUNGAY < _model.KH_LH_DENNGAY && b.KH_LH_DENNGAY > _model.KH_LH_TUNGAY && b.ID != _model.ID
                );

            }


            return IsBooking;
        }



        public void initialCategorySearchAction(ViewModelDKH_LH1 viewModel)
        {
            // ApplicationUser curUser = getCurrentUser();

            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => (o.DVGTVT_MAQL == viewModel.KH_LH_DVGTVT_MA || o.DVGTVT_MA == viewModel.KH_LH_DVGTVT_MA) && o.DVGTVT_ACTIVE == "1"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;

            //KH_MAs
            var khoahocs = Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN_LH1(viewModel.KH_LH_DVGTVT_MA,viewModel.KH_LH_KH_MA);  //_db.Filter<DKH>(o => o.KH_DVGTVT_MA == viewModel.KH_LH_DVGTVT_MA);
            ViewBag.KH_MAs = new SelectList(khoahocs, "KH_MA", "KH_TEN");

            var htdts = _db.GetAll<SHTDAOTAO>();
            //  ViewBag.HTDT_MAs = new SelectList(htdts, "HTDT_MA", "HTDT_MOTA");
            ViewBag.HANGDT_MAs = new SelectList(_db.Filter<SHANGDT>(o => o.HANGDT_RANK > 4), "HANGDT_MA", "HANGDT_TEN");
        }

        public void initialCategoryCreateAction()
        {
            ApplicationUser curUser = getCurrentUser();
            string _madvgtvt = curUser.MA_TINHTP;

            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40" && o.DVGTVT_MA == _madvgtvt && o.DVGTVT_ACTIVE == "1"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;
            ViewBag.HTDT_MAs = new SelectList(_db.GetAll<SHTDAOTAO>(), "HTDT_MA", "HTDT_MOTA");
            ViewBag.GD_MAs = new SelectList(_db.Filter<SGIAIDOAN>(o => o.GDOAN_MA != "LT"), "GDOAN_MA", "GDOAN_MOTA");

            ViewBag.HANGHT = " ";
            ViewBag.HANGGPLX = " ";
            string ma_DVGT = this.getCurrentUser().MA_TINHTP;
            ViewBag.KH_MAs = new SelectList(Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN_LH(ma_DVGT), "KH_MA", "KH_TEN");   //new SelectList(_db.Filter<DKH>(d => d.KH_DVGTVT_MA == ma_DVGT),"KH_MA","KH_TEN");

        }

        public void initialCategoryEditAction(CrudModelDKH_LH1_TH view)
        {

            // ApplicationUser curUser = getCurrentUser();
            ApplicationUser curUser = getCurrentUser();
            string _madvgtvt = curUser.MA_TINHTP;
            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40" && o.DVGTVT_MA == _madvgtvt && o.DVGTVT_ACTIVE == "1"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;
            ViewBag.HTDT_MAs = new SelectList(_db.GetAll<SHTDAOTAO>(), "HTDT_MA", "HTDT_MOTA");
            ViewBag.GD_MAs = new SelectList(_db.Filter<SGIAIDOAN>(o => o.GDOAN_MA != "LT"), "GDOAN_MA", "GDOAN_MOTA");

            //KH_MAs
            var khoahocs = Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN_LH1(view.KH_LH_DVGTVT_MA,view.KH_LH_KH_MA); //_db.Filter<DKH>(o => o.KH_DVGTVT_MA == view.KH_LH_DVGTVT_MA);
            ViewBag.KH_MAs = new SelectList(khoahocs, "KH_MA", "KH_TEN");

            //PT

            var lstPTinKH = new List<JsonDataDKH_GVinPT>();
            lstPTinKH = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_PT_IN_KH_BY_HT_TH(_madvgtvt, view.KH_LH_KH_MA, "");

            string str = "";
            for (int i = 0; i < lstPTinKH.Count; i++)
            {
                var a = lstPTinKH[i];
                str = str + "<li class='list-group-item'><input type='checkbox' name='chk_" + i.ToString() + "' style='font-weight: bold;font-size: 12px' checked value='" + a.PT_MA + "#" + a.GV_MA + "'</>" + a.PT_MA + "-" + a.GV_HOTEN + "</li>";
            }
            ViewBag.PT_MAs = str;

            //Week

            var kh = _db.GetOne<DKH>(o => o.KH_DVGTVT_MA == _madvgtvt && o.KH_MA == view.KH_LH_KH_MA);
            var dictWeekinDate = GPS_Library.GET_WEEK_DATERANGE(kh.KH_NGAYKG.Value, kh.KH_NGAYBG.Value);


            List<SelectListItem> lstItemSelect = new List<SelectListItem>();


            dictWeekinDate.Keys.ToList().ForEach(o => lstItemSelect.Add(new SelectListItem() { Value = o.ToString(), Text = "Tuần " + o.ToString() }));
            ViewBag.WEEKS = new SelectList(lstItemSelect, "Value", "Text");


        }
        #endregion
    }

    public class JsonStringTH
    {
        public string strKhoaHoc { get; set; }
        public string strgiangvien { get; set; }
        public string strphuongtien { get; set; }

    }

}